package com.kitchen.service.impl;

import com.kitchen.beans.entity.User;
import com.kitchen.beans.vo.UserVo;
import com.kitchen.common.utils.JwtUtils;
import com.kitchen.common.utils.MD5Utils;
import com.kitchen.common.utils.QfsyBeanUtils;
import com.kitchen.common.utils.SendCode;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.common.vo.ResultVO;
import com.kitchen.mapper.UserMapper;
import com.kitchen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO Resgit(UserVo userVo) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uUsername",userVo.getuUsername());
        List<User> users = userMapper.selectByExample(example);
        if (users.size()>0){
            return new ResultVO(ResStatus.NO, "用户名重复", null);
        }
        Example example1 = new Example(User.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("uPhone",userVo.getuPhone());
        List<User> userq = userMapper.selectByExample(example1);
        if (userq.size()>0){
            return new ResultVO(ResStatus.NO, "手机号重复", null);
        }
        else{
            List<User> users1 = QfsyBeanUtils.castToVoList(userVo, User.class);
            for (User user : users1) {
                String md5Pwd = MD5Utils.md5(user.getuPassword());
                user.setuPassword(md5Pwd);
                user.setuLogintime(new Date());
                user.setuRegisttime(new Date());
                int insert = userMapper.insert(user);
                if (insert>0){
                    return new ResultVO(ResStatus.OK, "success", null);
                }
                else {
                    return new ResultVO(ResStatus.NO, "error", null);
                }
            }
            return new ResultVO(ResStatus.NO, "error", null);
        }

    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO Login(UserVo uservVo) throws Exception {
        if (!uservVo.getuUsername().equals("")){
            User users = new User();
            users.setuUsername(uservVo.getuUsername());
            String pwd=MD5Utils.md5(uservVo.getuPassword());
            users.setuPassword(pwd);
            User selectOne = userMapper.selectOne(users);
            if (Objects.nonNull(selectOne)){
                selectOne.setuLogintime(new Date());
                int i = userMapper.updateByPrimaryKey(selectOne);
                return new ResultVO(ResStatus.OK, JwtUtils.createToken(uservVo.getuUsername(),selectOne.getuId().toString()), selectOne);
            }
        }
        else {
            User users = new User();
            users.setuPhone(uservVo.getuPhone());
            User selectOne = userMapper.selectOne(users);
            if (Objects.nonNull(selectOne)){
                selectOne.setuLogintime(new Date());
                int i = userMapper.updateByPrimaryKey(selectOne);
                return new ResultVO(ResStatus.OK, JwtUtils.createToken(uservVo.getuPhone(),selectOne.getuId().toString()), selectOne);
            }
        }
        return new ResultVO(ResStatus.NO, "error", null);
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO userinfolist(Integer uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        System.out.println(user.getuUsername());
        if (Objects.nonNull(user)){
            return new ResultVO(ResStatus.OK, "success", user);
        }else {
            return new ResultVO(ResStatus.NO, "error", null);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO updateinfo(UserVo userVo) {
        ResultVO userinfo = userinfolist(userVo.getuId());
        User user=(User)userinfo.getData();
        user.setuAddress(userVo.getuAddress());
        user.setuName(userVo.getuName());
        user.setuImg(userVo.getuImg());
        user.setuPhone(userVo.getuPhone());
        user.setuSex(userVo.getuSex());
        int i = userMapper.updateByPrimaryKey(user);
        if (i>0){
            return new ResultVO(ResStatus.OK, "success", null);
        }
        return new ResultVO(ResStatus.NO, "error", null);
    }


    @Override
    public ResultVO upsd(Integer uid, String password) {
        ResultVO userinfo = userinfolist(uid);
        User user=(User)userinfo.getData();
        user.setuPassword(MD5Utils.md5(password));
        int i = userMapper.updateByPrimaryKey(user);
        if (i>0){
            return new ResultVO(ResStatus.OK, "success", null);
        }
        return new ResultVO(ResStatus.NO, "error", null);
    }


}
